﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Sys;
using PagedList;
using BLL.Sys;
using Common;
using Enums;
using Enums.Sys;
using Model.Organization;
using BLL.Organization;
namespace DAGG.Controllers.Sys
{
    public class SysRoleController : BaseController
    {
        Sys_RoleBLL bll = new Sys_RoleBLL();
        EnterpriseBLL bll0 = new EnterpriseBLL();
        //
        // GET: /SysRole/
        public ActionResult Index(int page=1)
        {
            SysUserModel userModel = base.GetUserInfo();
            string strWhere = string.Empty;
            //递归获取其所属子公司和子公司的子公司
            if (EnumManagement.GetEnumValue(SysRoleEnum.SuperManageRole) == userModel.RoleId)//管理员角色 查看全部
                strWhere += "  ";
            else
            {
                var listWhere = string.Join(",", bll0.GetSonEnterprise(userModel.EnterpriseId).Select(obj => obj.ID));
                if (string.IsNullOrEmpty(listWhere))
                    strWhere += " and (EnterpriseId  in (0))";
                else
                    strWhere += " and (EnterpriseId in (" + listWhere + "))";
            }
            IPagedList<Sys_RoleModel> sysRoleList = bll.Getlist(strWhere).ToPagedList(page, 10);
            ViewBag.MyPageList = sysRoleList;
            return View();
        }
        public ActionResult Edit(int id = 0)
        {
            Sys_RoleModel model = new Sys_RoleModel();
            if (id > 0) model = bll.GetModelById(id);
            //绑定公司下拉框
            List<EnterpriseModel> list = bll0.GetSonEnterprise(1);
            List<SelectListItem> selectListItem = new List<SelectListItem>();

            foreach (var item in list)
            {
                bool Istrue = false;
                if (item.ID == model.EnterpriseId) Istrue = true;
                selectListItem.Add(new SelectListItem { Text = item.EnterpriseName, Value = item.ID.ToString(), Selected = Istrue });
            }
            ViewData["selectList"] = selectListItem;
            return View(model);
        }
        public ActionResult SaveData(Sys_RoleModel model)
        {
            ResultEntity entity;
            if (model.ID==0)
            {
                model.CreateDate = DateTime.Now;
                model.CreateLoginID = base.GetUserInfo().ID;
                model.CreateLoginName = base.GetUserInfo().RealName;
                model.ChangeDate = DateTime.Now;
                model.ChangeLoginID = base.GetUserInfo().ID;
                model.ChangeLoginName = base.GetUserInfo().RealName;
                model.ViewDel = EnumManagement.GetEnumValue(SysEnum.NoDele);
                model.ViewFlag = 1;
                model.EnterpriseId = base.GetUserInfo().EnterpriseId;
                entity = bll.AddRole(model);
            }
            else
            {
                model.ChangeDate = DateTime.Now;
                model.ChangeLoginID = base.GetUserInfo().ID;
                model.ChangeLoginName = base.GetUserInfo().RealName;
                entity = bll.UpdRole(model);
            }
            return Redirect("/SysRole/Index");
        }

	}
}